﻿using FasterWPF;
using FasterWPFExtras;
using FasterWPFDemo.UserControls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Xceed.Wpf;
using wpfToolkit = Xceed.Wpf.Toolkit;
using System.Windows.Controls.Primitives;
using System.IO;
using System.Windows.Markup;

namespace FasterWPFDemo
{
    //Xceed Grid
    public class TabItem07 : UserControl, IUserControlGuid
    {
        private string _userCtlGuid = null;

        public string UserControlGuid
        {
            get
            {
                return _userCtlGuid;
            }

            set
            {
                _userCtlGuid = value;
            }
        }

        public TabItem07()
        {
            //InitializeComponent(); XAML removed. Initialize via code-behind.

            this.Initialize(ContainerType.VerticalPanel);

            this.Width = 875D;
            this.Height = 750D;

            this.BeginSettings<UserControl>()
                .SetSelectorClass<UserControl>(0, 0, "tabLabel")
                .SetItemBorderColorAndThickness<UserControl>(Brushes.Gray, new Thickness(3)).EndSettings();
        }

        public void LoadUserControl()
        { 
            Xceed.Wpf.DataGrid.DataGridControl XceedGrid = null;

            ToolBar tbar1 = new ToolBar();
            tbar1.Initialize(500D, 50D, ContainerType.HorizontalPanel);
            tbar1.Background = Brushes.Orange;

            //these two should really be initialized as Composites
            Button tbtn1 = new Button();
            tbtn1.Initialize(130D, 40D, ContainerType.ContentControlPanel);
            tbtn1.ApplyButtonStrategy("White", "Gray", "Khaki", "Black", "Orange", "Gold", new Thickness(0), System.Windows.VerticalAlignment.Center, System.Windows.HorizontalAlignment.Center);
            
            Button tbtn2 = new Button();
            tbtn2.Initialize(130D, 40D, ContainerType.ContentControlPanel, "White", "Gray", "Khaki", "Black", "Orange", "Gold", new Thickness(0), System.Windows.VerticalAlignment.Center, System.Windows.HorizontalAlignment.Center);

            Button tbtn3 = new Button();

            tbar1.BeginSettings<ToolBar>()
                .Set<Button, ToolBar>(0, 0, "Width", 130D)
                .Set<Button, ToolBar>(0, 0, "Height", 40D)
                .Set<Button, ToolBar>(0, 0, "Content", "Get values from Grid")
                .Set<Button, ToolBar>(0, 1, "Width", 130D)
                .Set<Button, ToolBar>(0, 1, "Height", 40D)
                .Set<Button, ToolBar>(0, 1, "Content", "Add new row to Grid")
                .Set<Button, ToolBar>(0, 2, "Width", 130D)
                .Set<Button, ToolBar>(0, 2, "Height", 40D)
                .Set<Button, ToolBar>(0, 2, "Content", "Delete selected row")
                .EndSettings<ToolBar>();

            tbar1.BeginComposite<ToolBar>()
                .AddExisting<Button, ToolBar>(0, 0, tbtn1)
                .SubscribeEventOnChild<Button, ToolBar, MouseButtonEventArgs>(0, 0, "PreviewMouseDown", ((o, ev) =>
                {
                    List<DataRowView> idrvw = XceedGridExt.RetriveDataRows(XceedGrid);
                    foreach (object zx in idrvw) //XceedGrid.ItemsSource)
                    {
                        MessageBox.Show("ROWSTATE: " + ((DataRowView)zx).Row.RowState.ToString());
                        MessageBox.Show("VALUE: " + ((DataRowView)zx).Row.ItemArray.GetValue(0).ToString());
                        MessageBox.Show("VALUE: " + ((DataRowView)zx).Row.ItemArray.GetValue(1).ToString());
                        MessageBox.Show("VALUE: " + ((DataRowView)zx).Row.ItemArray.GetValue(2).ToString());
                        MessageBox.Show("VALUE: " + ((DataRowView)zx).Row.ItemArray.GetValue(3).ToString());
                    }
                }))
                .AddExisting<Button, ToolBar>(0, 1, tbtn2)
                .SubscribeEventOnChild<Button, ToolBar, MouseButtonEventArgs>(0, 1, "PreviewMouseDown", ((o, ev) =>
                {
                    XceedGrid.AddNewRow();
                }))
                .AddExisting<Button, ToolBar>(0, 2, tbtn3)
                .SubscribeEventOnChild<Button, ToolBar, MouseButtonEventArgs>(0, 2, "PreviewMouseDown", ((o, ev) =>
                {
                    XceedGrid.DeleteSelectedRow(() => { MessageBox.Show("Perform action on delete: <such as removing item from background collection?>"); });
                }))
                .EndComposite<ToolBar, ToolBarArgs>(null);

            ToolBar tbar21 = new ToolBar();
            tbar21.Initialize(300D, 50D, ContainerType.HorizontalPanel);
            tbar21.Background = Brushes.OrangeRed;

            Button tbtn21 = new Button();
            tbtn21.Initialize(40D, 50D, ContainerType.HorizontalPanel);
            tbtn21.Padding = new Thickness(0);
            tbtn21.ApplyButtonStrategy("White", "Gray", "Khaki", "Black", "Orange", "Gold", new Thickness(0, 0, 0, 0), System.Windows.VerticalAlignment.Center, HorizontalAlignment.Center);

            //tbtn21.BeginSettings<Button>()
            //      .SubscribeMouseSingleClickOnBorder<Button>((o, ev) =>
            //      {
            //          MessageBox.Show("Test 3");
            //      })
            //      .EndSettings<Button>();

            tbtn21.BeginComposite<Button>()
                  .AddImage<Button>(0, 0, @"pack://siteoforigin:,,,/Images/3.png", UriKind.Absolute, 30D)
                  .EndComposite<Button, ButtonArgs>(null);

            Border tbtn22 = new Border();
            tbtn22.Initialize(40D, 40D, ContainerType.HorizontalPanel, Brushes.Black, new Thickness(1), Brushes.Orange);
            tbtn22.SetMouseOverColorOnBorder(Brushes.Wheat);
            tbtn22.CornerRadius = new CornerRadius(10);

            EllipseGeometry myEllipseGeometry = new EllipseGeometry();
            myEllipseGeometry.Center = new Point(15, 15);
            myEllipseGeometry.RadiusX = 15;
            myEllipseGeometry.RadiusY = 15;

            tbtn22.BeginSettings<Border>()
                  .SetImageMargin<Border>(0, 0, new Thickness(4))
                  .SetImageClip<Border>(0, 0, myEllipseGeometry)
                  .SubscribeMouseSingleClickOnBorder<Border>((o, ev) =>
                  {
                      MessageBox.Show("Test 4");
                  })
                  .EndSettings<Border>();

            tbtn22.BeginComposite<Border>()
                  .AddImage<Border>(0, 0, @"pack://siteoforigin:,,,/Images/4.png", UriKind.Absolute, 30D)
                  .EndComposite<Border, BorderArgs>(null);

            Button tbtn25 = new Button();
            tbtn25.Initialize(130D, 40D, ContainerType.HorizontalPanel);
            tbtn25.ApplyButtonStrategy(
                "Purple", "GhostWhite", "White", "Brown", "Gray", "Red", "GhostWhite", "White", "Purple", "Black",  "Orange","GhostWhite", "White", "Gray", "Gold", new Thickness(0, 0, 0, 0), System.Windows.VerticalAlignment.Center, HorizontalAlignment.Center);

            tbar21.BeginSettings<ToolBar>()
             .Set<Button, ToolBar>(0, 2, "Width", 130D)
             .Set<Button, ToolBar>(0, 2, "Height", 40D)
             .Set<Button, ToolBar>(0, 2, "Content", "Validate Row")
             .EndSettings<ToolBar>();

            tbar21.BeginComposite<ToolBar>()
                .AddExisting<Button, ToolBar>(0, 0, tbtn21)
                .AddExisting<Border, ToolBar>(0, 1, tbtn22)
                .AddExisting<Button, ToolBar>(0, 2, tbtn25)
                .SubscribeEventOnChild<Button, ToolBar, MouseButtonEventArgs>(0, 0, "PreviewMouseDown", ((o, ev) =>
                {
                  // MessageBox.Show("Test 3");
                }))
                .SubscribeEventOnChild<Button, ToolBar, MouseButtonEventArgs>(0, 2, "PreviewMouseDown", ((o, ev) =>
                {
                    XceedGrid.ValidateRows(4, new Predicate<DataRowView>(
                        drv => { return (drv as DataRowView).Row[0].ToString() == ""; }),
                        "ERROR - Required Field!");
                }))
                .EndComposite<ToolBar, ToolBarArgs>(null);

            ToolBarTray tTray = new ToolBarTray();
            tTray.Background = Brushes.Gray;
            tTray.Initialize(790D, 100D, ContainerType.HorizontalPanel, Orientation.Horizontal, false);

            tTray.BeginComposite<ToolBarTray>()
                .AddExisting<ToolBar, ToolBarTray>(0, 0, tbar1)
                .EndComposite<ToolBarTray, ToolBarTrayArgs>(new ToolBarTrayArgs(0, 1));

            tTray.BeginComposite<ToolBarTray>()
                .AddExisting<ToolBar, ToolBarTray>(0, 0, tbar21)
                .EndComposite(new ToolBarTrayArgs(0, 0));

            Expander expa = new Expander();
            expa.IsExpanded = true;
            expa.Initialize(810D, 180D, ContainerType.VerticalPanel, new Label() { Content = "Expander with a Toolbar Tray inside", Background = Brushes.White});
            expa.BeginComposite<Expander>()
                .AddExisting<ToolBarTray, Expander>(0, 0, tTray)
                .AddText<Expander>(0, 1, "Some text beneath the toolbar tray")
                
                .AddFlatButton<Expander>(0, 2, 150D, 30D, "Change Expander", Brushes.Black, new Thickness(1), Brushes.Snow, new CornerRadius(3D), new Thickness(1), new Thickness(1), Brushes.Red, (o, args) =>
                {
                    expa.ChangeExpanderSkin(Brushes.LightSalmon, Brushes.LightSalmon, "Green", "Khaki", "Gold", "Purple", "Green", "Gold", "Orange", "M 0, 5 H 5 M 5, 0 V 5 Z", "M 1,1.5 L 4.5,5 L 8,1.5", ExpanderExt.Shape.Rectangle);

                })
                .EndComposite<Expander, ExpanderArgs>(null);
            expa.Background = Brushes.White;
            expa.ApplyExpanderStrategy("Orange", "Red", "Blue", "Purple", "Green", "Gold", "Transparent", ExpanderExt.Shape.Rectangle); // "M 0, 5 H 5 M 5, 0 V 5 Z", "M 1,1.5 L 4.5,5 L 8,1.5");





         

            XceedGrid = new Xceed.Wpf.DataGrid.DataGridControl();

            #region Build Sample DataSet
            DataSet ds2 = new DataSet("testDs");
            DataTable dsTestTable2 = new DataTable("Test");
            ds2.Tables.Add(dsTestTable2);
            dsTestTable2.Columns.Add(new DataColumn("Attraction", typeof(string)));
            dsTestTable2.Columns.Add(new DataColumn("Location", typeof(string)));
            dsTestTable2.Columns.Add(new DataColumn("Column 3", typeof(DateTime)));
            dsTestTable2.Columns.Add(new DataColumn("Column4", typeof(Decimal)));
            dsTestTable2.Columns.Add(new DataColumn("Validation Errors", typeof(string)));
            dsTestTable2.Columns.Add(new DataColumn(" ", typeof(string)));
            DataRow row12 = ds2.Tables[0].NewRow();
            DataRow row22 = ds2.Tables[0].NewRow();

            row12["Attraction"] = "";
            row12["Location"] = "Chicago, IL";
            row22["Attraction"] = "Lisle Arboretum";
            row22["Location"] = 5000.00.ToString("C2");
            row12["Column 3"] = DateTime.Today;
            row12["Column4"] = 500.00D;
            row22["Column 3"] = DateTime.Today;
            row22["Column4"] = 7000.00D;
            dsTestTable2.Rows.Add(row12);
            dsTestTable2.Rows.Add(row22);

            #endregion

            XceedGrid.Initialize(800D, 300D, ds2);
            XceedGrid.Columns[4].Width = 134D;
            XceedGrid.Columns[5].Width = 134D;
            // XceedGrid.Columns[5].Visible = false;

            StackPanel spkexpa = new StackPanel();
            spkexpa.Initialize(810D, 700D, ContainerType.VerticalPanel, Orientation.Vertical);
            spkexpa.Background = Brushes.WhiteSmoke;
            spkexpa.BeginComposite<StackPanel>()
                .AddExisting<Expander, StackPanel>(0, 0, expa)
                .AddExisting<Xceed.Wpf.DataGrid.DataGridControl, StackPanel>(0, 1, XceedGrid)
                .EndComposite<StackPanel, StackPanelArgs>(null);

            this.BeginComposite<UserControl>(_userCtlGuid)
                .AddLabel<UserControl>(0, 0, "Tab Item 7 -  ToolBars and Xceed DataGrid Example", Brushes.Orange)
                .AddExisting<StackPanel, UserControl>(0, 1, spkexpa)
                .EndComposite<UserControl, UserControlArgs>(null);
        }
    }
}
